package edu.ped.android.view;

import edu.ped.android.utils.GestureNFC;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

public class TrajectoryView extends View {

	private GestureNFC gesture;
	private int matriceHeight;
	private int matriceWidth;

	public TrajectoryView(Context context,	AttributeSet attrs) {
		super(context,attrs);
		gesture=null;
		matriceHeight=-1;
		matriceWidth=-1;
		LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 
				LayoutParams.WRAP_CONTENT);
		params.weight=0.2f;
		setLayoutParams(params);

	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if(matriceHeight>0 && matriceWidth>0 && gesture!=null){
			int tailleX=  (getWidth() /  matriceWidth);
			int tailleY=  (getHeight() /   matriceHeight);
			int taille = Math.min(tailleX, tailleY);
			for(int i=0;i<matriceWidth;i++)
				for(int j=0;j<matriceHeight;j++){
					Paint p = new Paint();
					int id = gesture.contains(i,j);
					if(id != -1){
						p.setColor(Color.GREEN);
						canvas.drawOval(new RectF(j*taille	, i*taille	, (j+1)*taille, (i+1)*taille), p);	
						p.setColor(Color.BLACK);
						p.setTextSize(40);
						id++;
						canvas.drawText(""+id, j*taille+taille/2-10	, i*taille+taille/2, p);
					}
					else{
						p.setColor(Color.RED);
						canvas.drawOval(new RectF(j*taille	, i*taille	, (j+1)*taille, (i+1)*taille), p);
					}
				}
		}
		invalidate();
	}

	public GestureNFC getTrajectory() {
		return gesture;
	}
	public void setTrajectory(GestureNFC trajectory) {
		this.gesture = trajectory;
	}
	public int getMatriceHeight() {
		return matriceHeight;
	}
	public void setMatriceHeight(int matriceHeight) {
		this.matriceHeight = matriceHeight;
	}
	public int getMatriceWidth() {
		return matriceWidth;
	}
	public void setMatriceWidth(int matriceWidth) {
		this.matriceWidth = matriceWidth;
	}
}
